package cn.demomaster.quickui_sample.ui.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.demomaster.quickcompat.util.QMUIDisplayHelper;
import com.demomaster.quickcompat.widget.dialog.QuickDialog;
import com.google.gson.Gson;

import cn.demomaster.quicklogger.QdLog;
import cn.demomaster.quickpicturer.activity.CameraIDCardActivity;
import cn.demomaster.quickpicturer.helper.PhotoHelper;
import cn.demomaster.quickui.annotation.ActivityPager;
import cn.demomaster.quickui.annotation.ResType;
import cn.demomaster.quickui_sample.R;

@ActivityPager(name = "身份证读取", preViewClass = TextView.class, resType = ResType.Resource)
public class IDCardActivity extends BaseActivity {

    public ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_idcard);
        imageView = findViewById(R.id.main_image);
        int navMenuHeight = QMUIDisplayHelper.getNavMenuHeight(mContext);
        Log.e("CGQ", "navMenuHeight:" + navMenuHeight);

        // 获取底部导航栏高度
        Resources resources = getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        int navigationBarHeight = resources.getDimensionPixelSize(resourceId);

        // 设置底部内边距
        View bottomLayout = findViewById(android.R.id.content);
        bottomLayout.setPadding(0, 0, 0, navigationBarHeight);
    }

    /**
     * 拍摄证件照片
     *
     * @param type 拍摄证件类型
     */
    private void takePhoto(final int type) {
//TODO        getPhotoHelper().takePhotoForIDCard(new PhotoHelper.OnTakePhotoResult() {
//            @Override
//            public void onSuccess(Intent data, String path) {
//                if (!TextUtils.isEmpty(path)) {
//                    imageView.setImageBitmap(BitmapFactory.decodeFile(path));
//                }
//                String imgBase64 = imageToBase64(path);
//                Log.i("CGQ", imgBase64);
//                Bundle extras = data.getExtras();
//                if (extras != null) {
//                    Bitmap photo = extras.getParcelable("data");
//                    imageView.setImageBitmap(photo);
//                    //photo = ImageUtils.toRoundBitmap(photo, fileUri); // ���ʱ���ͼƬ�Ѿ��������Բ�ε���
//                    //photo = ImageUtils.savePhoto(photo, Constants.APP_PATH_PICTURE, "")
//                    //iv_personal_icon.setImageBitmap(photo);
//                    //uploadPic(photo);
//                }
//            }
//
//            @Override
//            public void onFailure(String error) {
//
//            }
//        });
    }

    /**
     * 身份证正面
     */
    public void frontIdCard2(View view) {
        takePhoto(CameraIDCardActivity.TYPE_ID_CARD_FRONT);
    }

    /**
     * 身份证正面
     */
    public void frontIdCard(View view) {
//        getPhotoHelper().takePhotoForIDCard(new PhotoHelper.OnTakePhotoResult() {
//            @Override
//            public void onSuccess(Intent data, String path) {
//                if (!TextUtils.isEmpty(path)) {
//                    imageView.setImageBitmap(BitmapFactory.decodeFile(path));
//                }
//                String imgBase64 = imageToBase64(path);
//                Log.i("CGQ", imgBase64);
//            }
//
//            @Override
//            public void onFailure(String error) {
//
//            }
//        });
    }

    /**
     * 身份证反面
     */
    public void backIdCard2(View view) {
        takePhoto(CameraIDCardActivity.TYPE_ID_CARD_BACK);
    }

    /**
     * 身份证反面
     */
    public void backIdCard(View view) {
        SelectPhotoPopupWindow();
    }

    QuickDialog quickDialog;
    //选择照片的popupWindow
    private void SelectPhotoPopupWindow() {
        //@drawable/listview_option_menu
        quickDialog = new QuickDialog.Builder(mContext)
                .create();
        quickDialog.bindView(R.id.tv_title,"请选择你要上传的图片")
                .bindView(R.id.tv_left,"拍照", (dialog, view, tag) -> {
                    dialog.dismiss();
                    takePhoto2();
                })
                .bindView(R.id.tv_right,"从相册中选取", (dialog, view, tag) -> {
                    dialog.dismiss();
                    selectPhoto();
                });
        quickDialog.show();
    }

    private void selectPhoto() {
//        getPhotoHelper().selectPhotoFromGallery(new PhotoHelper.OnTakePhotoResult() {
//            @Override
//            public void onSuccess(Intent data, String path) {
//                QdLog.i("path=" + path);
//            }
//
//            @Override
//            public void onFailure(String error) {
//                QdLog.i("error=" + error);
//            }
//        });
    }

    private void takePhoto2() {
//        getPhotoHelper().takePhoto(null,new PhotoHelper.OnTakePhotoResult() {
//            @Override
//            public void onSuccess(Intent data, String path) {
//                Gson gson = new Gson();
//                String map_str2 = gson.toJson(data);
//                if (!TextUtils.isEmpty(path)) {
//                    imageView.setImageBitmap(BitmapFactory.decodeFile(path));
//                }
//
//                Bundle extras = data.getExtras();
//                if (extras != null) {
//                    Bitmap photo = extras.getParcelable("data");
//                    imageView.setImageBitmap(photo);
//                    //photo = ImageUtils.toRoundBitmap(photo, fileUri); // ���ʱ���ͼƬ�Ѿ��������Բ�ε���
//                    //photo = ImageUtils.savePhoto(photo, Constants.APP_PATH_PICTURE, "")
//                    //iv_personal_icon.setImageBitmap(photo);
//                    //uploadPic(photo);
//                }
//            }
//
//            @Override
//            public void onFailure(String error) {
//
//            }
//        });
    }

}
